﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Suteki.Common.Repositories;
using GeneCMS.Models.physical;

namespace GeneCMS.Models.Helpers
{
    public class CMSMembershipProvider : MembershipProvider
    {
        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            throw new NotImplementedException();
        }

        public bool CheckUserExists(string username)
        {
            Repository<Usr> data = new Repository<Usr>();
            return (data.GetAll().Where(u => u.Username == username).Count() > 0 );
        }

        public Usr GetUserModel(string username)
        {
            Repository<Usr> data = new Repository<Usr>();
            Usr usr_found = data.GetAll().Where(u => u.Username == username).SingleOrDefault();
            return usr_found;
        }

        private const string PASSPHRASE = "IWILLwalkinHisWaysfrom0to99901";
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {// Check username
            if (string.IsNullOrEmpty(username) || username.Length > 100)
            {
                status = MembershipCreateStatus.InvalidUserName;
                return null;
            }
            username = username.ToLower();
            if (this.CheckUserExists(username))
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return null;
            }
            Repository<Usr> data = new Repository<Usr>();
            Usr new_user = new Usr() { Username = username, Pswd = Encrypter.EncryptString(password, PASSPHRASE) };
            data.InsertOnSubmit(new_user);
            data.SubmitChanges();

            status = MembershipCreateStatus.Success;
            return this.GetUser(username, false);
        }

        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            throw new NotImplementedException();
        }

        public override bool EnablePasswordReset
        {
            get { throw new NotImplementedException(); }
        }

        public override bool EnablePasswordRetrieval
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
        {
            throw new NotImplementedException();
        }

        public override int GetNumberOfUsersOnline()
        {
            throw new NotImplementedException();
        }

        public override string GetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            Repository<Usr> data = new Repository<Usr>();
            Usr usr_found = (Usr) GetUserModel(username);
            if (usr_found == null)
            {
                return null;
            }
            else
            {
                DateTime empty_date = new DateTime();
                MembershipUser mbr_user = new MembershipUser("AspNetSqlMembershipProvider", usr_found.Username, null, null, null, null, true, false, empty_date, empty_date, empty_date, empty_date, empty_date);
                return mbr_user;
            }
        }

        public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
        {
            throw new NotImplementedException();
        }

        public override string GetUserNameByEmail(string email)
        {
            throw new NotImplementedException();
        }

        public override int MaxInvalidPasswordAttempts
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredNonAlphanumericCharacters
        {
            get { throw new NotImplementedException(); }
        }

        public override int MinRequiredPasswordLength
        {
            get { return 6; }
        }

        public override int PasswordAttemptWindow
        {
            get { throw new NotImplementedException(); }
        }

        public override MembershipPasswordFormat PasswordFormat
        {
            get { throw new NotImplementedException(); }
        }

        public override string PasswordStrengthRegularExpression
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresQuestionAndAnswer
        {
            get { throw new NotImplementedException(); }
        }

        public override bool RequiresUniqueEmail
        {
            get { throw new NotImplementedException(); }
        }

        public override string ResetPassword(string username, string answer)
        {
            throw new NotImplementedException();
        }

        public override bool UnlockUser(string userName)
        {
            throw new NotImplementedException();
        }

        public override void UpdateUser(MembershipUser user)
        {
            throw new NotImplementedException();
        }

        public override bool ValidateUser(string username, string password)
        {
            var user = GetUserModel(username);
            bool password_is_correct = false;
            if (user != null)
                password_is_correct = Encrypter.EncryptString(password, PASSPHRASE) == user.Pswd;
            return password_is_correct;
        }
    }
}
